<style>
</style>
<template>
  <list_url ref="list_url" :config="config">
    <template #action="{row,col}">
      <el-button v-for="btn in action_row(row, col)" :type="btn.type" size="small" @click="btn.onclick(row)">{{ btn.title }}</el-button>
    </template>
  </list_url>

  <el-dialog v-model="edit_test.show" width="80%" :draggable="true" style="margin: auto;margin-top: 10vh;">
    <p>标题：{{edit_test.execute.title}}</p>
    <p>执行详情：{{edit_test.execute.condition_info}}</p>
    <p class="flex">执行数据主体：<text_one v-model:value="edit_test.form_data.model_name"></text_one></p>
    <p class="flex">数据ID：<text_one v-model:value="edit_test.form_data.link_id" ></text_one></p>
    <div class="flex" style="justify-content: right;">
      <el-button type="primary" @click="to_test">提交数据</el-button>
    </div>
  </el-dialog>
</template>
<script>
import {iframe, in_right_admin} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import {_config, decode_btns, s94_event} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";
import {vue_template} from "@/js/dom_tool.js";
import list_id from "@/components/auto_import/UnifyParam/list_id.vue";
import Text_one from "@/components/auto_import/UnifyParam/text_one.vue";

export default {
  components: {Text_one, list_id},
  props: ['__iframe'],
  data(){
    let app = this, btns = {}, is_del = this.$get('is_del');
    let model_name = this.$get('model_name', '');

    if(in_right_admin('Execute/add')) btns['添加'] = function(){ iframe('execute/edit') }
    if(in_right_admin('Execute/delete')) btns['彻底删除'] = function(){ app.$refs['list_url'].delete() }

    let function_map = {};
    ajax({url:'/admin/execute/config_map',cache:true,async:false},function (res){
      function_map = res.data.function_map;
      return false;
    })
    let forms_config = {
      title: {label:'标题',name:'where[title][like]',type:'text_one',value:''},
      model_name: {label:'模型限制',name:'model_name',type:'text_one',value:model_name},
    };

    let list_cols = {
      id: {name:'id', label:'ID', width:'65px', fixed:'left'},
      title: {name:'title', label:'标题',type:'text_more', width:'160px'},
      condition_info: {name:'condition_info', label:'执行详情', type:'text_more', min_width: '200px'},
      limit_model_name: {name:'limit_model_name', label:'模型限定', type:'text_more', width:'120px'},
      bind_event_name: {name:'bind_event_name', label:'绑定事件', type:'text_more', width:'120px'},
      need_log: {name:'need_log', label:'是否记录', type:'radio_switch', width:'80px'},
      throw: {name:'throw', label:'抛出异常', type:'radio_switch', width:'80px'},
      async: {name:'async', label:'异步执行', type:'radio_switch', width:'80px'},
      retry_max: {name:'retry_max', label:'最多重试', type:'text_one', width:'80px'},
    };
    if (is_del){
      list_cols['delete_time'] = {name:'delete_time', label:'删除时间', width:'165px', type:'datetime'};
    }else {
      list_cols['create_time'] = {name:'create_time', label:'创建时间', width:'165px'};
    }
    if (!_config('this_tenant.id')){
      list_cols['tenant_id'] = {name:'tenant_id', label:'租户', width:'65px', fixed:'right'};
    }
    list_cols['action'] = {name:'action', label:'操作', width:'180px', type:'action', fixed:'right'};

    return {
      config: {
        api: '/admin/execute/'+(is_del?'recycle':'list'),
        title: "事件列表"+(is_del?'(已删除)':''),
        btns: btns,
        forms_config: forms_config,
        list_cols: list_cols,
        resdata: function(list){
          list.forEach(item=>{
            item.condition_info = app.condition_info(item);
          })
          return list;
        },
        limit: 20,
        p: this.$get('p', 1),
      },
      function_map: function_map,
      edit_test: {show: false, execute: {}, form_data:{}},
    }
  },
  methods: {
    action_row(row, col){
      let res = {}, app=this;
      let can_edit = in_right_admin('Execute/edit') && ~[0,row.tenant_id].indexOf(_config('this_tenant.id'))
      if (can_edit){
        res['编辑'] = function (row){
          let param = {
            id: row.id,
          }
          iframe('execute/edit?'+s94Mjs.param_encode(param))
        }
      }else {
        res['详情'] = function (row){
          let param = {
            id: row.id,
            is_info: 1,
          }
          iframe('execute/edit?'+s94Mjs.param_encode(param))
        }
      }
      if (in_right_admin('Execute/edit_test')){
        res['测试事件'] = (row)=>{
          app.open_test(row)
        };
      }
      return decode_btns(res);
    },
    condition_info(condition){
      if (!this.function_map[condition.function]) return '';
      let fuc = this.function_map[condition.function];
      let param = JSON.parse(condition.param);
      Object.assign(param, fuc.info_param);
      return vue_template(fuc.info, param);
    },
    open_test(row){
      this.edit_test.show = true;
      this.edit_test.execute = row;
      this.edit_test.form_data.id = row.id;
    },
    to_test(){
      ajax({url:'/admin/execute/edit_test', data:this.edit_test.form_data})
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    // 监听修改和添加操作
    s94_event(['execute/add','execute/edit'], (evt)=>{
      let res = evt.data;
      if (res.data) this.$refs.list_url.list();
    })
  },
  unmounted() {
    // 监听修改和添加操作
    s94_event(['execute/add','execute/edit'], false)
  }
}
</script>
